package engine.display.Story 
{
	import engine.GameData;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.display.InterceptMsgLogic;
	import engine.event.CustomEvent;
	import engine.guide.GuideEvent;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.FunctionGuideUI;
	import engine.newUi.NpcTalkUI;
	import engine.ui.AlertUI;
	import engine.ui.UIManage;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;

	/**
	 * ...
	 * @author WangHaiBo  www.woniu.com
	 */
	public class StoryLogic 
	{
		private static var _self		:StoryLogic;
		private var _bindManage			:BindManage;
		private var _model				:ModelLocator;
		private var _storyType			:int;
		private var storyMax			:int	=	300;
		private var storyComplet		:int	=	-1;
		private var completeStoryObj	:Object;
		private var storyListObj		:Object;
		public  var nowStoryID:String	=	"";
	
		public var  comStoryID:String = "";
		public function StoryLogic() 
		{
			Init();
		}
		public static function getInstance():StoryLogic
		{
			if (_self == null) 
			{
				_self	=	new StoryLogic();
			}
			return _self;
		}
		private function Init():void
		{
			storyListObj			=	{ };
			completeStoryObj		=	{ };
			readAllStory("Story_" + StaticIni.getIniVar("LastStoryID","r"));
			readAllStory("Story_" + StaticIni.getIniVar("LastSupperStoryID","r"));
			storyListObj["Story_500010"]	=	StaticIni.getIniObj("Story_500010");
			_model		=	ModelLocator.getInstance();
			_bindManage	=	BindManage.getInstance();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_FIRST_LOGIN), firstLoginHandle);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_TASK_ACCEPT_SUCCESS), taskAcceptSuccessHandle);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_TASK_COMPLETE_SUCCESS), taskCompleteSuccessHandle);
			_model.addEventListener(String(CustomEvent.SWITCH_SCENE), switchSceneHandle);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_FB_KILL_BOSS), killBossHandle);
			storyComplet	=	_bindManage.BindRecord(_model.scene, "GutCompleted", addFunction, null);
		}
		/**
		 * 杀死BOSS后
		 * @param	e
		 * 对象
		 * 胜负 0：负 1： 胜
		 */
		private function killBossHandle(e:CustomMsgEvent):void 
		{
			if (e.pList[1] == 1) 
			{
				_storyType	=	StoryType.KILL_BOSS_KO;
				
				if( checkServerInfor(_storyType, e.pList[0]) )
				{
					InterceptMsgLogic.getInstance().intercept( String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_ACTIONECTYPE_INFO) );
				}
			}		
		}
		/**
		 * 切换场景
		 */
		private function switchSceneHandle(e:CustomEvent):void
		{
			_storyType	=	StoryType.CHANGE_SCENE;
			checkServerInfor(_storyType, String(GameData.SceneConfig) );
		}
		/**
		 * 成功完成任务
		 * @param	e
		 */
		private function taskCompleteSuccessHandle(e:CustomMsgEvent):void
		{
			_storyType	=	StoryType.TASK_COMPLETE
			checkServerInfor(_storyType, e.pList[0]);
		}
		/**
		 * 接取成功任务
		 * @param	e
		 */
		private function taskAcceptSuccessHandle(e:CustomMsgEvent):void 
		{
			_storyType	=	StoryType.TASK_ACCEPT;
			checkServerInfor(_storyType, e.pList[0]);
		}
		/**
		 * 第一次登陆游戏
		 * @param	e
		 */
		private function firstLoginHandle(e:CustomMsgEvent):void 
		{
			_storyType	=	StoryType.FIRST_LOGIN;
			//StoryMoveUI.getInstance().showTalkInfo("Story_0001");
		}
		/**
		 * 根据消息来检测具体对应的剧情动画
		 * @param	type
		 * @param	data
		 */
		private function checkServerInfor(type:int,key:String):Boolean
		{
			nowStoryID	=	"";
			for (var name:String in storyListObj) 
			{
				if (storyListObj[name] != null)
				{
					if (storyListObj[name]["type"] == type && storyListObj[name]["key"] == key) 
					{
						if (storyListObj[name]["preid"] == "0" ) 
						{
							nowStoryID	=	name;
							break;
						}
						else if (completeStoryObj["Story_"+storyListObj[name]["preid"]] != null)
						{
							nowStoryID	=	name;
							break;
						}
					}
				}
			}
			if (nowStoryID != "")
			{
				if(nowStoryID == "Story_500010")
				{
					if(completeStoryObj[[nowStoryID]] == null)
					{
						FunctionGuideUI.getInstance().setOtherGuide("ShowPicGuide_1");
						FunctionGuideUI.getInstance().showOrHide();
						SendStoryID(nowStoryID);
					}
				}
				else
				{
					StoryMoveUI.getInstance().showTalkInfo(nowStoryID);
					SendStoryID(nowStoryID);
				}
				
				return true;
			}
			return false;
		}
		/**
		 * 读取所有的剧情生成一张表
		 */
		private function readAllStory(storyID:String):void
		{
			var nextStory:String;
			var stroyObj:Object		=	stroyObj	=	StaticIni.getIniObj(storyID);
			if (stroyObj != null) 
			{
				storyListObj[storyID]	=	stroyObj
				if(stroyObj["preid"] != 0)
				{
					readAllStory("Story_" + stroyObj["preid"])
				}
				else
				{
					deleteCompleteStory()
				}
			}
		}
		/**
		 * 删除样同剧情
		 */
		public function deleteCompleteStory():void 
		{
			for (var name:String in completeStoryObj) 
			{
				if (completeStoryObj[name] != null) 
				{
					storyListObj[name] 	= null;
				}
			}
		}
		/**
		 * 添加剧情
		 * @param	PropertiesName
		 * @param	PropertiesValue
		 * @param	data
		 */
		private function addFunction(PropertiesName:String,PropertiesValue:*,data:*):void 
		{
			//var objName:String	=	"Story_" + "0000".slice(0, 4-String(PropertiesValue).length) + String(PropertiesValue)
			completeStoryObj[PropertiesValue]	=	PropertiesValue
			deleteCompleteStory();
		}
		/**
		 * 完成了某一剧情
		 * @param	val 剧情号
		 */
		public function SendStoryID(val:String):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADD_GUT_COMPLETED, val);
			
			_model.dispatchEvent( new GuideEvent( GuideEvent.COMPLETE_STORY, [ val ] ) );
			comStoryID = val;
			if( val == "Story_500001" )
			{//第一个剧情，需要自动能够导航接任务
				
//				TaskTrackingUI.getInstance().autoAccept();
			}
		}
	}

}